package cn.jiyun.controller;

import cn.jiyun.pojo.Users;
import cn.jiyun.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(tags = "提供用户相关的接口访问")
public class UserController {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UsersService usersService;

    @GetMapping("/queryList")
    public String queryUserList(Model model){
        List<Users>users=usersService.queryUsers();
        model.addAttribute("list",users);
        return "list";
    }

    @GetMapping("/query")
    @ResponseBody
    @ApiOperation(value = "查询用户信息", notes = "查询用户信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名")
    })
    public Object queryUser(@RequestParam(required = true) String name) {
        List<Users> users = usersService.queryUsers();
        logger.info("查询用户信息{}{}{}",name,name,name);
        return users;
    }


    @GetMapping("/addUserPage/{pagePath}")
    public Object addUserPage(@PathVariable String pagePath){
        return pagePath;
    }

    @PostMapping("/addUser")
    @ResponseBody
    public Object addUser(Users users){
        System.out.println(users);
       int i= usersService.addUsers(users);
       return "添加成功"+i;
    }
    @PostMapping("/login")
    public String login(Model model,Users users){
        Users users1=usersService.login(users);
        if (users1!=null){
            return "redirect:/queryList";
        }
        return "redirect:/addUserPage/add";
    }
}
